// import * as Cesium from '@cesium/Source/Cesium.js';
import {
  createGuid,
  defined,
  defaultValue,
  destroyObject,
  DeveloperError,
  EventHelper,
  Model,
  PrimitiveCollection,
  ScreenSpaceEventHandler,
  CesiumTerrainProvider,
  EllipsoidTerrainProvider,
  IonResource,
  knockout
} from "@cesiumjs";

class LayerViewModel {

  constructor(viewer, callback) {
    if (!defined(viewer)) {
      throw new DeveloperError("viewer is required");
    }

    const _this = this;
    const scene = viewer.scene;

    this.callback = callback;

    Object.defineProperty(this, "viewer", {
      get() {
        return viewer
      }
    })

    this.imageryLayers = [];
    this.terrainLayers = [];

    // 底图加载完成后的事件
    const helper = new EventHelper();
    function tileLoadProgressHandler(event) {
      if (event === 0) {
        _this._updateLayers();
      }
    }
    helper.add(scene.globe.tileLoadProgressEvent, tileLoadProgressHandler);
    _this._updateLayers();
    
    this.destroy = function(){
      helper.removeAll();
    }
  }

  emit(type, layers) {
    this.callback && this.callback({
      type,
      layers
    })
  }

  // destroy() {
    // return destroyObject(this);
  // }

  updateProperty(type, prop, fnVal) {
    this.imageryLayers.forEach((e, i) => {
      const v = fnVal(i);
      e[prop] !== v && (e[prop] = v)
    })
    this.emit("imageryLayers", this.imageryLayers)
  }

  _updateLayers() {
    this._updateImageryLayers();
    this._updateTerrainLayers();
  }

  _updateImageryLayers() {
    this.imageryLayers = []
    const layers = this.viewer.imageryLayers;
    const count = layers.length;
    for (let i = count - 1; i >= 0; --i) {
      const layer = layers.get(i);
      if (!layer.name) {
        let provider = layer.imageryProvider;
        let url = provider.url;
        if (!url) {
          provider = provider._imageryProvider;
          url = (provider && provider.url);
        }
        layer.name = url;
      }

      !layer.name && (layer.name = "[未命名]");
      !layer.title && (layer.title = layer.name);

      !layer.guid && (layer.guid = createGuid());
      this.imageryLayers.push(layer)
    }

    this.emit("imageryLayers", this.imageryLayers)
  }

  _updateTerrainLayers() {
    const {
      viewer
    } = this;
    const layer = viewer.terrainProvider;
    const realLayers = viewer.terrainProvider._layers;
    const realShow = !!(realLayers && realLayers.length > 0);
    if (!layer.name && realShow) {
      layer.name = realLayers[0].resource._url + realLayers[0].tileUrlTemplates;
    }!layer.name && (layer.name = "[默认地形]");
    !layer.title && (layer.title = layer.name);
    // 定义show属性
    !defined(layer.show) && Object.defineProperties(layer, {
      show: {
        get: function() {
          return realShow;
        },
        configurable: true
      },
    });

    !layer.guid && (layer.guid = createGuid());

    this.terrainLayers = [layer]

    this.emit("terrainLayers", this.terrainLayers)
  }
}

export default LayerViewModel;
